#include "toolbutton.h"

ToolButton::ToolButton(QGraphicsScene *scene)
    : QGraphicsPixmapItem()
{
    scene->addItem(this);

    iconItem = new QGraphicsPixmapItem(this,scene);
    iconItem->setPos(4,4);

    buttonUp.load(RESOURCE_DIR + "/buttons/Kit_ButtonUp.png");
    buttonDown.load(RESOURCE_DIR + "/buttons/Kit_ButtonDown.png");

    setPixmap(buttonUp);

    buttonState = false;
    toggleButton = true;
}

ToolButton::ToolButton(int recharge, QString caption, QPixmap icon, QGraphicsScene *scene)
    : QGraphicsPixmapItem()
{
    rechargeTimeMax = recharge;
    rechargeTimeCurrent = 0;

    scene->addItem(this);

    iconItem = new QGraphicsPixmapItem(icon,this,scene);
    iconItem->setPos(4,4);
    setAcceptHoverEvents(true);

    buttonUp.load(RESOURCE_DIR + "/buttons/Kit_ButtonUp.png");
    buttonDown.load(RESOURCE_DIR + "/buttons/Kit_ButtonDown.png");

    setPixmap(buttonUp);
    
    rechargeOverlay = new QGraphicsRectItem(0,0,pixmap().width(), pixmap().height(),this,scene);
    rechargeOverlay->setPen(Qt::NoPen);
    rechargeOverlay->setBrush(QBrush(QColor(0,0,255,160),Qt::SolidPattern));
    rechargeOverlay->setVisible(false);

    //Setup the tool tip
    toolTipText = scene->addText(caption,QFont("SansSerif",9,QFont::Bold));
    toolTip = scene->addRect(0,0,toolTipText->boundingRect().width(),toolTipText->boundingRect().height()-2);
    toolTip->setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));
    toolTip->setVisible(false);
    toolTip->setZValue(1);
    toolTipText->setParentItem(toolTip);
    toolTipText->setPos(0,-1);
    toolTipText->setAcceptHoverEvents(false);

    buttonState = false;
    toggleButton = true;
}

ToolButton::~ToolButton()
{
    iconItem->~QGraphicsItem();
}

void ToolButton::setIcon(QPixmap icon)
{
    iconItem->setPixmap(icon);
}

void ToolButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(rechargeTimeCurrent > 0)
        return;


    if (buttonState)
    {
        setPixmap(buttonUp);
        iconItem->setPos(4,4);
        buttonState = !buttonState;
        emit clicked(this);
    }
    else
    {
        setPixmap(buttonDown);
        iconItem->setPos(6,6);
        buttonState = !buttonState;
        emit clicked(this);
    }
}

void ToolButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (!toggleButton)
    {
        setPixmap(buttonUp);
        iconItem->setPos(4,4);
        buttonState = false;
    }
}

void ToolButton::releaseButton(bool toolused)
{
    if(toolused)
    {
        rechargeTimeCurrent = rechargeTimeMax;
        QRectF bjeeber = rechargeOverlay->rect();
        bjeeber.setHeight(pixmap().height() * (rechargeTimeCurrent / rechargeTimeMax));
        bjeeber.setY(pixmap().height() - bjeeber.height());
        rechargeOverlay->setRect(bjeeber);
    }
        setPixmap(buttonUp);
        iconItem->setPos(4,4);
        buttonState = false;
}

void ToolButton::setToggleButton(bool toggle)
{
    toggleButton = toggle;
}

bool ToolButton::isToggleButton()
{
    return toggleButton;
}

bool ToolButton::getSate()
{
    return buttonState;
}

void ToolButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->setPos(event->scenePos().x(),event->scenePos().y()+20);
    toolTip->setVisible(true);
}

void ToolButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->setVisible(false);
}

void ToolButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->setPos(event->scenePos().x(),event->scenePos().y()+20);
}

void ToolButton::advance(int phase)
{
    if(phase != 0)
       return;
    if(rechargeTimeCurrent > 0 && !buttonState)
    {
        rechargeTimeCurrent -= .05;
    }
    else if(rechargeTimeCurrent < 0)
        rechargeTimeCurrent = 0;

    if(rechargeTimeCurrent > 0)
        rechargeOverlay->setVisible(true);
    else
        rechargeOverlay->setVisible(false);

    QRectF bjeeber = rechargeOverlay->rect();
    bjeeber.setHeight(pixmap().height() * (rechargeTimeCurrent / rechargeTimeMax));
    bjeeber.setY(pixmap().height() - bjeeber.height());

    rechargeOverlay->setRect(bjeeber);


}
